/*
 * @Author: yusha
 * @Date: 2023-06-13 15:33:41
 * @LastEditors: yusha
 * @LastEditTime: 2023-07-11 17:06:35
 * @Description: 接口
 */
import axios from 'axios';
import { message } from 'antd';
import { serviceConfig } from './swaggerServiceConfig';

/** 接口前缀 */
const newConfig = { ...serviceConfig };
/** 获取token */
const getUserInLocal = () => {
    const token = window.localStorage.getItem('token');
    if (!token) return;
    return token;
};
const axiosOemPurchase = axios.create({
    baseURL: 'https://titancenter-api.sniffgroup.com',
    headers: { 
        'X-Token': getUserInLocal()??'' ,
        'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8',
    }
});

// 请求拦截
axiosOemPurchase.interceptors.request.use(function (config) {
    config.headers = config.headers ?? {};
    const isPost = ['POST', 'post'].includes(config.method!);
    // const token = getUserInLocal();
    // if (token) {
    //     config.headers['token'] = token;
    // } else {
    //     window.location.href = '/opsManager/login';
    // }
    if (isPost) {
        if (!config.data && config.params) {
            config.data = config.params;
        }
        config.headers['Content-Type'] = 'application/json';
    }
    return config;
});

// 返回拦截
axiosOemPurchase.interceptors.response.use(function (response) {
    if (response.data.code === 200 || response.data.code === '0' || response.data.code == 0 ) return response;

    // if (['20001', '20002', '24010061', '25000001', '25000002'].includes(response.data.code)) {

    //     message.error(response.data.msg);

    //     setTimeout(() => {
    //         window.location.href = '/opsManager/login';
    //     }, 1000);
    //     return response;
    // }

    // if(['26140001'].includes(response.data.code)){
    //   message.error(response.data.msg);
    // }

    return Promise.reject(response.data.msg);


}, function (error) {
    message.error('请求错误：' + String(error));
    return Promise.reject(error);
});

export const getUsers = (params) => {
  return axiosOemPurchase({ url: '/test/baseData/list', method: 'POST', params });
};